package function.client;

import cls.annotation.Autowired;
import function.Result;
import function.client.adaptor.UserFacadeQueryUserInfoAdaptor;
import function.client.adaptor.UserFacadeRewardPointsAdaptor;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * 用户相关远程服务调用client
 *
 * @author G_Y
 * @version 1.0.0, 2022/3/29 15:33
 **/
@Component
public class SimpleUserClient extends AbstractClient {

    @Autowired
    private UserFacadeQueryUserInfoAdaptor queryUserInfoAdaptor;

    @Autowired
    private UserFacadeRewardPointsAdaptor rewardPointsAdaptor;

    /**
     * 查询用户信息
     *
     * @author G_Y, 2022/3/29 16:04
     **/
    public Result<RespUserInfo> queryUserInfo(QueryUserRequest request) {

        // 实体转换
        QueryUserInfoDTO reqDto = this.exchange(request);

        RpcResult<UserInfoDTO> rpcResult = super.invoke(queryUserInfoAdaptor, reqDto);

        // 不符合预期，invoke方法会统一返回null
        if (Objects.isNull(rpcResult)) {
            return Result.failed();
        }

        // 成功，实体转换
        RespUserInfo respUserInfo = this.exchange(rpcResult.getData());

        return Result.success(respUserInfo);

    }

    /**
     * 奖励用户积分
     *
     * @author G_Y, 2022/3/29 16:05
     **/
    public Result<Void> rewardPoints(rewardPointsRequest request) {

        // 实体转换
        rewardPointsRequestDTO reqDto = this.exchange(request);

        RpcResult<Boolean> rpcResult = super.invoke(rewardPointsAdaptor, reqDto);

        // 不符合预期，invoke方法会统一返回null
        if (Objects.isNull(rpcResult)) {
            return Result.failed();
        }

        return Result.success();

    }

    private QueryUserInfoDTO exchange(QueryUserRequest request) {
        return null;
    }

    private RespUserInfo exchange(UserInfoDTO request) {
        return null;
    }

    private rewardPointsRequestDTO exchange(rewardPointsRequest request) {
        return null;
    }
    // other methods ...

}
